<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use com\gdl\newagk\interfaces\AdminQuestionsServiceClient;
use com\gdl\newagk\dataobject\adminquestions\QuestionsDTO; 

/**
 * 试题接口
 * @author tc 2018/05/07
 */

class Adminquestions extends Base
{
 	/** 
 	 *  试题列表   
     *  @param  admin_reg or plaformId
 	 *  @return JSON [description] 400失败 200成功
 	 */
	public function questionsList() 
    {
    	$msg = ['code'=>400, 'msg'=>"暂无数据", 'data'=>[], 'count'=>0];
    	$plaformId = $this->request->param("plaformId") ?$this->request->param("plaformId") : 0;
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$recovery = $this->request->param("recovery") ? $this->request->param("recovery") : ""; # recovery
    	$name = $this->request->param("name") ? $this->request->param("name"):""; # id题干模糊查询
    	$plafName = $this->request->param("plafName") ? $this->request->param("plafName"):""; # 所属平台名模糊查询
    	$qbankId = $this->request->param("qbankId") ? $this->request->param("qbankId") : ""; # 赛选题库id
    	$sectionId = $this->request->param("sectionId") ? $this->request->param("sectionId") : ""; #章节id
    	$type = $this->request->param("type") ? $this->request->param("type") : ""; # 题目类型
    	$level = $this->request->param("level") ? $this->request->param("level") : ""; # 题目难度
    	$status = $this->request->param("status") ? $this->request->param("status"):""; #是否禁用 # 1启用 2禁用 3 平台禁用
    	$plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
    	$offset = $this->request->param("offset") ? $this->request->param("offset"): 0;
    	$num = $this->request->param("num") ? $this->request->param("num"): 10;
    	$plaformId = $plaformChildId ? $plaformChildId : $plaformId;
    	if($plaformId==0){
    		$adminReg = getApiAdminReg($plaformId,'all');
    	}else{
			$adminReg = getApiAdminReg($plaformId,'child');
    	}
    	# 查询出题库id
    	$adminExpArray = explode("','",$adminReg);
    	$wheres['admin_reg'] = array('in',$adminExpArray); 
    	$qbankIdList = Db::name('qbank')->where($wheres)->field('id')->select();
    	$qbankInArray = [];
    	for($i=0;$i<count($qbankIdList); $i++){
    		$qbankInArray[] = $qbankIdList[$i]['id'];
    	} 
    	$inStr = implode(",",$qbankInArray); 
        try {
        	if($recovery=='recovery'){
        		$where="agk_questions.isdelete = 1 and agk_questions.qbankId in (".$inStr.") ";	
        	}else{
        		$where="agk_questions.isdelete = 0 and agk_questions.qbankId in (".$inStr.") ";
        	}
        	if($qbankId && $qbankId!=0){
        		$where.=" and agk_questions.qbankId=".$qbankId;
        	}
        	if($sectionId && $sectionId!=0){
        		$where.=" and agk_questions.sectionId=".$sectionId;
        	}
        	if($type && $type!=0){
        		$where.=" and agk_questions.typeId=".$type;
        	}
        	if($level && $level!=0){
        		$where.=" and agk_questions.level=".$level;
        	}
        	if($status==1){
        		$where.=" and agk_questions.status=1";
        	}
        	if($status==2){
        		$where.=" and agk_questions.status=0";
        	}
        	if($status==3){
        		$where.=" and agk_questions.status=2";
        	}

        	// if($name){
        	// 	$where.=" and (agk_questions.question like '%{$name}%' or agk_questions.id like '%{$name}%') ";	
        	// }
        	// var_dump($where);exit;
        	# 所属平台名模糊查询
        	if($plafName){
        		//plafName
        	} 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminQuestionsServiceClient(NULL);
            \ApiClient::build($api);
            $QuestionsDO = new QuestionsDTO(); 
            
            $QuestionsDO->offset = $offset;
            $QuestionsDO->num = $num; 
            $QuestionsDO->update = $name;
            $QuestionsDO->where = $where; 
            $result = $api->questionsList($QuestionsDO);  
            if ($result->success == TRUE) {
            	$msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
                $msg['count'] = $result->datas[0]['count'] ? $result->datas[0]['count'] : 0;
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    /**
     * 试题详情
     */
    public function questionInfo(){
    	$msg = ['code'=>400, 'msg'=>'获取详情失败', 'data'=>[]];
    	$plaformId = $this->request->param("palformId") ? $this->request->param("palformId") : "";
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$id = $this->request->param("id") ? $this->request->param("id") : "";
    	if(empty($id)){
    		$msg['msg'] = "缺少必要参数";
    		return json_encode($msg);
    	}
    	try{
    		\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminQuestionsServiceClient(NULL);
            \ApiClient::build($api);
            $QuestionsDO = new QuestionsDTO(); 
            $QuestionsDO->id = $id; 
            $result = $api->questionInfo($QuestionsDO); 
            if($result->success == true){
                $msg['code'] = $result->code;
                $msg['msg'] = $result->message;
                $msg['data'] = $result->data[0];    
                if($result->code==200){  
                    $result->data[1]['quesTag']= json_decode($result->data[1]['quesTag'],true);
                    $result->data[1]['quesTagVal'] = json_decode($result->data[1]['quesTagVal'],true); 
                    $msg['data'] = $result->data[1];    
                }
            }

    	}catch(Exception $e){
    		$msg['msg'] = $ex->getMessage();
    	}
    	return json_encode($msg);
    }

    /**
     * 清空html标签的 试题详情
     */
    public function notQuestionsInfo(){
    	$msg = ['code'=>400, 'msg'=>'获取详情失败', 'data'=>[]];
    	$plaformId = $this->request->param("palformId") ? $this->request->param("palformId") : "";
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$id = $this->request->param("id") ? $this->request->param("id") : "";
    	$type = $this->request->param("all")?$this->request->param("all"):"all";
    	if(empty($id)){
    		$msg['msg'] = "缺少必要参数";
    		return json_encode($msg);
    	}
    	try{
    		\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminQuestionsServiceClient(NULL);
            \ApiClient::build($api);
            $QuestionsDO = new QuestionsDTO(); 
            $QuestionsDO->id = $id; 
            $QuestionsDO->where = $type; 
            $result = $api->questionInfo($QuestionsDO); 
            if($result->success == true){
                $msg['code'] = $result->code;
                $msg['msg'] = $result->message;
                $msg['data'] = $result->data[0];    
                if($result->code==200){  
                    $result->data[1]['quesTag']= json_decode($result->data[1]['quesTag'],true);
                    $result->data[1]['quesTagVal'] = json_decode($result->data[1]['quesTagVal'],true); 
                    $result->data[1]['answer'] = deleteHtml($result->data[1]['answer']);
                    $result->data[1]['describe'] = deleteHtml($result->data[1]['describe']);
                    $result->data[1]['question'] = deleteHtml($result->data[1]['question']);
                    $result->data[1]['select'] = deleteHtml($result->data[1]['select']);
                    $result->data[1]['selectArray'] = json_decode($result->data[1]['selectArray']);
                    $msg['data'] = $result->data[1];    
                }
            }

    	}catch(Exception $e){
    		$msg['msg'] = $ex->getMessage();
    	}
    	return json_encode($msg);
    }

	/**
     * 添加试题
     */
    public function addQuestion(){
    	$msg = ['code'=>400, 'msg'=>'添加失败', 'data'=>[]];
    	$plaformId = $this->request->param("palformId") ? $this->request->param("palformId") : "";
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$data = $this->request->post() ? $this->request->post() : "";
    	$datas = [
			'typeId'=>$data['typeId'],
			'qbankId'=>$data['qbankId'],
			'question'=>$data['question'],
			'select'=>$data['select'],
			'selectNumber'=>$data['selectNumber'],
			'answer'=>$data['answer'],
			'describe'=>$data['describe'],
			'knowsName'=>$data['knowsName'],
			'sectionId'=>$data['sectionId'],
			'quesTag'=>$data['quesTag'],
			'quesTagVal'=>$data['quesTagVal'],
			'level'=>$data['level'],
			'status'=>$data['status'],
		 ];
    	if(!is_array($data)){
    		$msg['msg'] = "请将试题内容完善";
    		return json_encode($msg);
    	}
    	try{
    		\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminQuestionsServiceClient(NULL);
            \ApiClient::build($api);
            $QuestionsDO = new QuestionsDTO(); 
            $QuestionsDO->id = $plaformId; 
            $QuestionsDO->data= $datas;
            $result = $api->addQuestion($QuestionsDO); 
            if($result->success == true){
                $msg['code'] = $result->code;
                $msg['msg'] = $result->message;  
            }

    	}catch(Exception $e){
    		$msg['msg'] = $ex->getMessage();
    	}
    	return json_encode($msg);
    }
    /**
     * 修改试题
     */
    public function editQuestion(){
    	$msg = ['code'=>400, 'msg'=>'修改试题失败', 'data'=>[]];
    	$plaformId = $this->request->param("palformId") ? $this->request->param("palformId") : "";
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$data = $this->request->post() ? $this->request->post() : "";
    	$id = $this->request->param("id") ? $this->request->param("id") : "";

    	if(!is_array($data)){
    		$msg['msg'] = "请将试题内容完善";
    		return json_encode($msg);
    	}
    	if(empty($id)){
    		$msg['msg'] = "请带入正确的试题标识";
    		return json_encode($msg);
    	}
    	$datas = [
			'typeId'=>$data['typeId'],
			'qbankId'=>$data['qbankId'],
			'question'=>$data['question'],
			'select'=>$data['select'],
			'selectNumber'=>$data['selectNumber'],
			'answer'=>$data['answer'],
			'describe'=>$data['describe'],
			'knowsName'=>$data['knowsName'],
			'sectionId'=>$data['sectionId'],
			'quesTag'=>$data['quesTag'],
			'quesTagVal'=>$data['quesTagVal'],
			'level'=>$data['level'],
			'status'=>$data['status'],
		 ]; 
		 // $datas = [
			// 'typeId'=>1,
			// 'qbankId'=>208,
			// 'question'=>'题干改11了111',
			// 'select'=>'A.个人工器具;B.服装;C.劳动防护用品;D.作业保护;E.第六个选项1',
			// 'selectNumber'=>5,
			// 'answer'=>'B',
			// 'describe'=>'解析',
			// 'knowsName'=>'hahha',
			// 'sectionId'=>104,
			// 'quesTag'=>'1001',
			// 'quesTagVal'=>'1003',
			// 'level'=>2,
			// 'status'=>1,
		 // ];
    	try{
    		\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminQuestionsServiceClient(NULL);
            \ApiClient::build($api);
            $QuestionsDO = new QuestionsDTO(); 
            $QuestionsDO->id = $id; 
            $QuestionsDO->data= $datas;
            $result = $api->editQuestion($QuestionsDO); 
            if($result->success == true){
                $msg['code'] = $result->code;
                $msg['msg'] = $result->message;  
            }
    	}catch(Exception $e){
    		$msg['msg'] = $e->getMessage();
    	}
    	return json_encode($msg);
    }
    /**
     * 获取该题库的章节
     */
    public function getSectionList(){
    	$msg = ['code'=>400, 'msg'=>'暂无题库章节', 'data'=>[]];
    	$qbankId = $this->request->param("qbankId") ? $this->request->param("qbankId"): "";
    	if(empty($qbankId)){
    		$msg['msg'] ="请带入正确的题库标识";
    		return json_encode($msg);
    	}
    	try{
    		$where['isdelete'] = 0;
    		$where['status'] = 1;
    		$where['qbankId'] = $qbankId;
    		$sectionList = Db::name('sections')->where($where)->field("id,seName")->select();
    		if($sectionList){
    			$msg['code'] = 200;
    			$msg['msg'] = "获取成功";
    			$msg['data'] = $sectionList;
    		}

    	}catch(Exception $e){
    		$msg['msg'] = $e->getMessage();
    	}
    	return json_encode($msg);
    }
    /**
     * 获取题目类型
     */
    public function getQuestypeList(){
    	$qbankId = $this->request->param("qbankid") ? $this->request->param("qbankid") : "";
    	$msg = ['code'=>400, 'msg'=>'获取失败','data'=>[]];
    	if(empty($qbankId)){
    		$msg['msg'] = "请带入正确的题库标识";
    		// return json_encode($msg);
    	}

    	try{
    		$where['isdelete'] = 0;
    		$where['status'] = 1;
    		$typeList = Db::name('questype')->where($where)->field('id,type')->order('id ASC')->select();
    		if($typeList){
    			$msg['code'] = 200;
    			$msg['msg'] = "获取成功";
    			$msg['data']= $typeList;
    		}
    	}catch(Exception $e){
    		$msg['msg'] = $e->getMessage();
    	}
    	return json_encode($msg); 
    }
    /**
     * 获取该平台下题库列表
     */
    public function getQbankList(){
    	$msg = ['code'=>400, 'msg'=>'未获取数据', 'data'=>[]];
    	$plaformId = $this->request->param("plaformId") ? $this->request->param("plaformId") : 0;
    	$qbankId = $this->request->param("qbankId") ? $this->request->param("qbankId") : "";
    	$admin_reg = $this->request->param("reg") ? $this->request->param("reg") : "";

    	if( empty($admin_reg)){
    		$msg['msg'] = "缺少必要参数";
    		// return json_encode($msg);
    	}

    	try{
    		$qbankModel = Db::name('qbank');
    		$where['isdelete'] = 0;
    		$where['status'] = 1;
    		if(!empty($qbankId)){
    			$where['id'] = array('neq',$qbankId);
    		}
    		if($plaformId!=0){
    			$where['plaformId'] = $plaformId;	
    		} 
    		$qbankList = $qbankModel->where($where)->field("id,name")->select();
    		if($qbankList){
    			$msg['code'] = 200;
    			$msg['msg'] = "获取成功";
    			$msg['data'] = $qbankList;
    		}
    	}catch(Exception $e){
    		$msg['msg'] = $e->getMessage();
    	}
    	return json_encode($msg);
    } 
     /**
     * 修改题库状态
     * status 0：禁用 1:启用  2：平台禁用
     */
    public function editStatus(){
        $msg = ['code'=>400, 'msg'=>"修改失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $id = $this->request->param('id') ? $this->request->param('id') : ""; # 1,2,3
        $status = $this->request->param('status') ? $this->request->param('status') : "0";
      
        if(empty($id)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        }

        try{

            $where['id'] = ["in", $id];
            if($plaformId==0){
                if($status==0){ # 禁用
                    $upstatus = Db::name('questions')->where($where)->setField(['status'=>2]);
                }else{ # 启用
                    $upstatus = Db::name('questions')->where($where)->setField(['status'=>1]);
                }

            # 子平台修改状态
            }else{ 
                $upstatus = Db::name('questions')->where($where)->setField(['status'=>$status]);
            }
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "禁用成功";
                if($status==1){
                    $msg['msg'] = "启用成功";
                }
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
    /**
     * 是否删除题目
     * 1:删除  0：还原
     */
    public function deleteQuestion(){
        $msg = ['code'=>400, 'msg'=>"操作失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $id = $this->request->param('id') ? $this->request->param('id') : ""; # 1,2,3
        $status = $this->request->param('delete') ? $this->request->param('delete') : "0"; # 0 还原 1删除 2彻底删除
        if(empty($admin_reg)){
            $msg['msg'] = "请先登录";
            // return json_encode($msg);
        }
        if(empty($id)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        }

        try{

        	$where['id'] = ["in", $id];

        	# 不允许删除试题
        	// if($status==1){
        	// 	$delQuestionId = Db::name('questions')->where($where)->field('id,qbankId')->select();
        	// 	$qbankIdList = [];
        	// 	foreach ($delQuestionId as $key => $value) {
        	// 		$qbankIdList[] = $value['qbankId'];
        	// 	}
        	// 	$paperWhere['dbs'] = ['in',$qbankIdList];
	        //     $paperList = Db::name('paper')->where($paperWhere)->field('id')->select();
	        //     if($paperList){
	        //         $msg['msg'] = "删除的试题包含已生成的试卷，请先彻底删除相关试卷";
	        //         return json_encode($msg);
	        //     }
        	// }
            $admin = Db::name("admin_user")->where(['id'=>1])->field('reg')->find();
            
            if($plaformId==0){
                if($admin_reg!=$admin['reg']){
					$msg['msg'] = "你没有权限修改";
                    // return json_encode($msg);
                }
                $upstatus = Db::name('questions')->where($where)->setField(['isdelete'=>$status]);
            # 子平台修改状态
            }else{ 
                $upstatus = Db::name('questions')->where($where)->setField(['isdelete'=>$status]);
            }
            if($status==2){
            	$upstatus = Db::name('questions')->where($where)->delete();
            	# 继续删除答案表
        		foreach ($DeleteResult as $key => $value) {
        			$deleteId[] = $value['id'];
        		} 
        		$answerWhere['questionsId'] = ['in',$id];
        		$answerRet = Db::name('questions_answer')->where($answerWhere)->delete();	
            }
            if($upstatus){
            	# 删除之后重新统计题库
                $msg['code'] = 200;
                $msg['msg'] = "成功";
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
    /**
     * execl 导出赛选的试题
     * @return [type] [description]
     */
    public function QuestionsDownExecl()
    {  
    	ob_end_clean(); # 清空缓冲区
    	# start 赛选开始
    	$msg = ['code'=>400, 'msg'=>"暂无数据", 'data'=>[]];
    	$plaformId = $this->request->param("plaformId") ?$this->request->param("plaformId") : 0;
    	$admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg") : "";
    	$recovery = $this->request->param("recovery") ? $this->request->param("recovery") : ""; # recovery
    	$name = $this->request->param("name") ? $this->request->param("name"):""; # id题干模糊查询
    	$plafName = $this->request->param("plafName") ? $this->request->param("plafName"):""; # 所属平台名模糊查询
    	$qbankId = $this->request->param("qbankId") ? $this->request->param("qbankId") : ""; # 赛选题库id
    	$sectionId = $this->request->param("sectionId") ? $this->request->param("sectionId") : ""; #章节id
    	$type = $this->request->param("type") ? $this->request->param("type") : ""; # 题目类型
    	$level = $this->request->param("level") ? $this->request->param("level") : ""; # 题目难度
    	$status = $this->request->param("status") ? $this->request->param("status"):""; #是否禁用
    	$plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
    	$select = $this->request->param("selectId") ? $this->request->param("selectId") : "all"; #选中导出 1,2,3全选导出selectId=all
		$plaformId = $plaformChildId ? $plaformChildId : $plaformId;
    	if($plaformId==0){
    		$adminReg = getApiAdminReg($plaformId,'all');
    	}else{
			$adminReg = getApiAdminReg($plaformId,'child');
    	}
    	# 查询出题库id
    	$adminExpArray = explode("','",$adminReg);
    	$wheres['admin_reg'] = array('in',$adminExpArray); 
    	$qbankIdList = Db::name('qbank')->where($wheres)->field('id')->select();
    	$qbankInArray = [];
    	for($i=0;$i<count($qbankIdList); $i++){
    		$qbankInArray[] = $qbankIdList[$i]['id'];
    	} 
    	$inStr = implode(",",$qbankInArray); 
    	if($recovery=='recovery'){
    		$where="a.isdelete = 1 and a.qbankId in (".$inStr.") ";	
    	}else{
    		$where="a.isdelete = 0 and a.qbankId in (".$inStr.") ";
    	}
    	if($qbankId && $qbankId!=0){
    		$where.=" and a.qbankId=".$qbankId;
    	}
    	if($sectionId && $sectionId!=0){
    		$where.=" and a.sectionId=".$sectionId;
    	}
    	if($type && $type!=0){
    		$where.=" and a.typeId=".$type;
    	}
    	if($level && $level!=0){
    		$where.=" and a.level=".$level;
    	}
    	if($status){
    		$where.=" and a.status=".$status;
    	}
    	if($name){
    		$where.=" and (a.question like '%".$name."%' or a.id like '%".$name."%') ";	
    	}
    	if($select!="all"){
    		$where.= " and (a.id in (".$select."))";
    	}
    	# 所属平台名模糊查询
    	if($plafName){
    		//plafName
    	}
    	# end list赛选 
        $header = ['归属题库','归属章节','题型','题干','选项','参考答案','专业解析','知识点','启用状态','题目难度']; 
        try {
        	$quesModel = Db::name('questions');
        	$filed = "b.name,c.seName,a.typeId,a.question,a.select,a.answer,a.knowsName,a.describe,a.status,a.level";
            $lists = $quesModel
				->alias('a')
				->join("qbank b", "a.qbankId = b.id", 'left')
				->join("sections c", "a.sectionId = c.id", 'left')
				->field($filed)
                ->where($where)->select(); 
            if($lists){
                $list = [];
                foreach($lists as $v){
                    $list[] = [
                    	$v['name'],
                    	$v['seName'],
                        getTypeId($v['typeId']),
                        deleteHtml($v['question']),
                        deleteHtml($v['select']),
                        $v['answer'],
                        deleteHtml($v['describe']),
                        $v['knowsName'],
                        getStatus($v['status']),
                        getLeve($v['level'])
                    ];
                }
            }else{
                $msg['msg'] = "没有可导出的试题";
                return json_encode($msg);
            }
        } catch (Exception $e) {
            $msg['msg'] = $e->getMessage();
            return json_encode($msg);
        } 
        $name = "我的试题";
        $version = '2007';
        $width = array('A'=>30,'D'=>100,'E'=>100);
        \Excel::oneExport($header, $list, $name, $version,$width);
    }
    /**
    * 一键删除所有回收站试题 
    */
   public function deleteAllQuestion(){
   		$msg = ['code'=>400,'msg'=>'清空失败','data'=>[]];
   		$plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        if($plaformId==0){
    		$adminReg = getApiAdminReg($plaformId,'all');
    	}else{
			$adminReg = getApiAdminReg($plaformId,'one');
    	}
    	$adminReg = explode("','",$adminReg);
        try{
        	$questionModel = Db::name('questions');
        	$where['isdelete'] = 1;
        	$where['admin_reg'] = ['in',$adminReg];
        	$deleteId = [];
        	# 先查出id再删
        	$DeleteResult = $questionModel->where($where)->field('id')->select();
        	
        	$result = $questionModel->where($where)->delete();
        	if($result){
        		# 继续删除答案表
        		foreach ($DeleteResult as $key => $value) {
        			$deleteId[] = $value['id'];
        		} 
        		$answerWhere['questionsId'] = ['in',$deleteId];
        		$answerRet = Db::name('questions_answer')->where($answerWhere)->delete();
        		$msg['msg'] = "清空成功";
        		$msg['code'] = 200;
        	}
        }catch(Exception $e){
        	$msg['msg'] = $e->getMessage();
        }	
        return json_encode($msg); 
   }

}